﻿using MyProject.Core;
using MyProject.Entities.Config;
using MyProject.Services.MyNavigation;
using MyProject.Views.Auth;
using MyProject.Views.UserList;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;


    public class AuthPageContext
    {
        // Страница, к которой прикреплен контент
        private readonly AuthPage _page;

        private readonly MyNavigationService _navigationService;
        private readonly AppConfig _appConfig;


        // Форма авторизации
        public MyProject.Entities.Auth.Auth LoginForm { get; } = new ();

        // Обработчик для кнопки авторизации
        public ICommand? LogInCommand { get; } = null;

        public AuthPageContext(AuthPage page, MyNavigationService navigationService) 
        {
            this._page = page;
            this._navigationService = navigationService;
            this._appConfig = AppConfig.getInstance();

           
            this.LogInCommand = new RelayCommand(o => { this._LoginIn(); });
        }

        // Обработчик для выполнения команды авторизации
        private void _LoginIn()
        {
            if (!ValidationHelper.IsFormValid(this.LoginForm, this._page))
            {
                return;
            }

           

            if (this._appConfig.AdminLogin == null || this._appConfig.AdminPassword == null)
            {
                MessageBox.Show("Файл конфигурации настроен неверно!");
                return;
            }

            if (this._appConfig.AdminLogin != this.LoginForm.Login || this._appConfig.AdminPassword != this.LoginForm.Password)
            {
                MessageBox.Show("Логин или пароль введены неверно!");
                this.LoginForm.ClearPassword();
                return;
            }

            this._navigationService?.navigate(new UserList(this._navigationService));
        }
    }
